# -*- coding: utf-8 -*-
#
# @Author: CPS
# @Date: 2020-09-22 14:29:41
# @Last Modified by: CPS
# @Last Modified time: 2020-09-24 15:52:19
# @file_path Z:\CPS\IDE\SublimeText\3176x32前端\Data\Packages\testt\testtlib\testtlib.py
# @Filename testtlib.py
#
from subprocess import Popen, PIPE

print('lib被引入')

def is_stylus(filename):
    #通过 file_name 判断 stylus
    if filename.endswith('.stylus') or filename.endswith('.styl'):
        return True

def is_vue(filename):
    #通过 file_name 判断 vue
    return filename.endswith('.vue')

def is_html(filename):
    #通过 file_name 判断 html
    if filename.endswith('.html') or filename.endswith('.xml'):
        return True

def is_pug(filename):
    #通过 file_name 判断 pug
    if filename.endswith('.pug'):
        return True

def check_stynax(fn):
    if is_stylus(fn):
        return 'stylus'
    if is_vue(fn):
        return 'vue'
    if is_html(fn):
        return 'html'
    if is_pug(fn):
        return 'pug'
    return False

# shell
def run(command,shell=False):
    try:
        p = Popen(
            command,
            stdout=PIPE,
            stdin=PIPE,
            stderr=PIPE,
            shell=shell
        )
    except Exception:
        raise Exception('run执行命令出错啦:',command)

    else:
        res, err = p.communicate()

        if err: return err.decode('utf-8').strip()

        return res.decode('utf-8').strip()

    return False

def node(strBuffer,command):
    try:
        p = Popen(
            command,
            stdout=PIPE,
            stdin=PIPE,
            stderr=PIPE,
            shell=True
        )
    except Exception:
        raise Exception('执行命令出错啦:',command)

    # 执行,同时指定返回接收的容器
    stdout, stderr = p.communicate(input=strBuffer.encode('utf-8'))

    if stdout:
        return stdout.decode('utf-8')
    else:
        return ('出错啦: \n%s' % stderr.decode('utf-8'))


def cmd(command):
    return run(command,shell=True)

def check_node():
    return run(['node', '-v'],shell=True)



if (__name__ == "__main__"):
    print(check_node())